﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nature.Data.CkMarkup
{
    public partial class CkThermoForm : IThermo7
    {
        public IEnumerable<AtomsPerMoleculeAmount> AtomsPerMoleculeAmounts
        {
            get 
            {
                return
                from el in this.ElementsCollection
                group el by el.ElementID into g
                select new AtomsPerMoleculeAmount
                {
                    SpeciesID = this.SpeciesID,
                    ElementID = g.Key,
                    AtomsPerMolecule = g.Sum(i => i.TotalAmount)
                };
            }
        }




        #region IThermo7 Members

        string IThermo7.SpeciesID
        {
            get { return this.SpeciesID; }
        }

        ReadOnlyArray<Thermo7Range> IThermo7.Ranges
        {
            get 
            {
                var items = from r in Thermo7Set.RangeCollection
                            let a = r.Coefficients.ToList()
                            select new Thermo7Range
                            {
                                A1 = a[0],
                                A2 = a[1],
                                A3 = a[2],
                                A4 = a[3],
                                A5 = a[4],
                                A6 = a[5],
                                A7 = a[6]
                            };
                Thermo7Range[] ranges = null;
                if (Thermo7Set.Temperatures.Any())
                {
                    ranges = items.ToArray();
                    throw new NotImplementedException();
                }
                else
                {
                    ranges = items.Reverse().ToArray();
                    var rlow = ranges[0];
                    var rhigh = ranges[1];
                    rlow.Tmin = this.Tmin.Value;
                    rhigh.Tmin = rlow.Tmax = this.Tmid.Value;
                    rhigh.Tmax = this.Tmax.Value;
                    ranges[0] = rlow;
                    ranges[1] = rhigh;
                    return ranges;
                }
            }
        }

        #endregion
    }
}
